package com.lebang.controller.buss;

import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.entity.buss.FinanceRecord;
import com.lebang.entity.task.TaskComments;
import com.lebang.example.FinanceRecordExample;
import com.lebang.example.TaskCommentsExample;
import com.lebang.service.buss.CustomService;
import com.lebang.service.buss.FinanceRecordService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：任务收退款管理控制层
 *
 * @author HWQ
 * @date Thu Apr 25 09:27:12 CST 2019
 */

@Controller
@RequestMapping("/financeRecord")

public class FinanceRecordController {

    @Autowired
    private FinanceRecordService financeRecordService;
    @Autowired
    private CustomService customService;

    /**
     * 财务页面
     */
    @Login
    @GetMapping("/recordlist")
    public String recordlist() {
        return "/financerecord/financerecord-list-finance";
    }

    /**
     * 业务员页面
     */
    @Login
    @GetMapping("/creatlist")
    public String creatlist(Model model) {
        model.addAttribute("customlist", customService.getAllCustomList());
        return "/financerecord/financerecord-list-creat";
    }

    /**
     * 分页接口
     **/
    @PostMapping("/page")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public Map<String, Object> page(Integer page, Integer limit, FinanceRecord financeRecord, String name, String star, String end) {
        Map<String, Object> list = new HashMap<>(1);
        int startNum = (page - 1) * limit;

        try {
            List<Map<String, Object>> data = financeRecordService.findpage(startNum, limit, financeRecord, name, star, end);
            Long count = financeRecordService.findcout(financeRecord, name, star, end);
            list.put("data",data);
            list.put("count",count);
            list.put("code",0);
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            list.put("data",null);
            list.put("count",null);
            list.put("code",200);
            return list;
        }

    }

    /**
     * 新增修改保存
     **/
    @PostMapping("/edit")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public JsonData edit(FinanceRecord po, HttpSession session) {
        if (po.getId() == null) {
            po.setStatus(1);
            po.setCheckstatus(1);
            po.setCreateBy(session.getAttribute("userid").toString());
            return financeRecordService.insert(po);
        } else {
            return financeRecordService.update(po);
        }
    }

    /**
     * 新增修改跳转
     **/
    @GetMapping("/edit")
    @Login
    @DataSource("commonDB")
    public String edit(@RequestParam(name = "id", required = false) Integer id, String taskId, String category,
                       Model model, String custom_id) {
        if (!"".equals(id) && null != id) {
            model.addAttribute("item", financeRecordService.selectByPrimaryKey(id));
        } else {
            FinanceRecord financeRecord = new FinanceRecord();
            financeRecord.setTaskId(taskId);
            financeRecord.setCategory(category);
            financeRecord.setCustomId(custom_id);
            model.addAttribute("item", financeRecord);
        }
        return "/financerecord/financerecord-edit";
    }

    /**
     * 财务确认
     **/
    @PostMapping("/confirm")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public JsonData confirm(@RequestParam(name = "idlist", required = true) String idlist, String note, HttpServletRequest request) {
        try {
            if (!TextUtil.isEmpty(idlist)) {
                if (!TextUtil.isEmpty(note)) {
                    note = note.replace("请输入备注", "无");
                }
                List<String> list = Arrays.asList(idlist.split(","));
                int i = financeRecordService.confirm(list, request.getSession().getAttribute("userid").toString(), note);
                if (i > 0) {
                    return Callback.success("success", "操作成功", null);
                } else {
                    return Callback.fail("error", "操作失败", null);
                }
            } else {
                return Callback.fail("error", "参数异常", null);
            }
        } catch (Exception e) {
            return Callback.fail("error", "未知错误 ", e.getMessage());
        }

    }

    /**
     * 作废
     **/
    @PostMapping("/cancel")
    @ResponseBody
    @Login
    @DataSource("commonDB")
    public JsonData cancel(@RequestParam(name = "idlist", required = true) String idlist, HttpServletRequest request) {
        try {
            if (!TextUtil.isEmpty(idlist)) {
                List<String> list = Arrays.asList(idlist.split(","));
                int i = financeRecordService.cancel(list, request.getSession().getAttribute("userid").toString());
                if (i > 0) {
                    return Callback.success("success", "操作成功", null);
                } else {
                    return Callback.fail("error", "操作失败", null);
                }
            } else {
                return Callback.fail("error", "参数异常", null);
            }
        } catch (Exception e) {
            return Callback.fail("error", "未知错误 ", e.getMessage());
        }

    }

    /**
     * 任务支出页面
     */
    @Login
    @GetMapping("/missionExpenditure")
    public String missionExpenditure() {
        return "/missionExpenditure";
    }
    @Login
    @ResponseBody
    @PostMapping("/missionExpenditure")
    @DataSource("commonDB")
    public Map<String, Object> missionExpenditurePage(HttpRequest request, Integer page, Integer limit) {
        Integer star = (page - 1) * limit;
        Map<String, Object> list = new HashMap<>(1);
        list.put("star", star);
        list.put("limit", limit);
        return financeRecordService.missionExpenditurePage(list);
    }

    /**
     * 财务审核提现列表
     */
    @Login
    @PostMapping
    @ResponseBody
    public Map<String, Object> outLayPage(HttpRequest request) {
        return null;
    }


    @Login
    @PostMapping("/data")
    @ResponseBody
    @DataSource("commonDB")
    public HashMap data(Long page, Integer limit,String taskId){
        HashMap map = new HashMap(1);
        FinanceRecordExample example = new FinanceRecordExample();
        FinanceRecordExample.Criteria c = example.createCriteria();
        example.setOffset((page - 1) * limit);
        example.setLimit(limit);
        example.setOrderByClause("create_time desc");
        c.andHasDeletedEqualTo(0);
        if(!TextUtil.isEmpty(taskId)){
            c.andTaskIdEqualTo(taskId);
        }

        map.put("count", financeRecordService.count(example));
        map.put("code", 0);
        List<FinanceRecord> masters = financeRecordService.selectAll(example);
        map.put("data", masters);
        return map;

    }


}